package com.ruoyi.function.controller;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.function.domain.Technician;
import com.ruoyi.function.domain.bo.SysUserBo;
import com.ruoyi.function.domain.bo.TechnicianSearchReqBo;
import com.ruoyi.function.domain.vo.UserListVo;
import com.ruoyi.function.domain.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.function.service.IUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/function/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;


    /**
     * 查询用户信息列表
     */
//    @PreAuthorize("@ss.hasPermi('function:user:list')")
    @GetMapping("/listB")
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('function:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(userService.selectUserByUserId(userId));
    }
    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('function:user:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateUser(@RequestBody SysUser user)
    {
        return toAjax(userService.updateUser(user));
    }
    /**
     * 获取用户详情
     */
    @GetMapping("/getMyinfo")
    public AjaxResult getMyinfo()
    {
        Long userId = getUserId();
        UserVo user = userService.getMyinfo(userId);
        return AjaxResult.success(user);
    }


    /**
     * 查询用户信息列表
     */

    @GetMapping("/list")
    public TableDataInfo list( TechnicianSearchReqBo technician)
    {
        startPage();
        List<UserListVo> list = userService.selectUserListVo(technician);
        return getDataTable(list);

    }

    /**
     *
     * 按服务类别查询
     */
    @GetMapping("/listByServiceType")
    public TableDataInfo listByServiceType(TechnicianSearchReqBo technician)
    {
        startPage();
        List<UserListVo> list = userService.listByServiceType(technician);
        return getDataTable(list);

    }


    /**
     * 修改用户信息
     */
    @PostMapping("/edit")
    public AjaxResult edit( @RequestBody SysUserBo userBo)
    {
        //获取当前用户id
        SysUser user = new SysUser();
        BeanUtils.copyProperties(userBo, user);
        Long userId = SecurityUtils.getUserId();
        user.setUserId(userId);
        if (null !=userBo.getModelIdArray()){
            user.setModelId(String.join(",",userBo.getModelIdArray()));
        }

        return toAjax(userService.updateUser(user));
    }

}
